package com.lzw.mall.order.controller;

import com.lzw.mall.common.entity.Member;
import com.lzw.mall.common.vo.OrderSessionUtil;
import com.lzw.mall.common.vo.ResultVO;
import com.lzw.mall.common.entity.MemberAddress;
import com.lzw.mall.common.service.IMemberAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/information")
public class InformationController {

    @Autowired
    private IMemberAddressService memberAddressService;



    @GetMapping("/")
    public String index() {
        return "information";
    }

    @GetMapping("/memberAddress")
    public String memberAddress(Model model){
        Member m = OrderSessionUtil.getCurrent();
        List<MemberAddress> addess=memberAddressService.selectByMemberId(m.getId());
        model.addAttribute("addess",addess);
        return "address";
    }

    @GetMapping("/order")
    public String order(){
        return "order";
    }

    @PostMapping("/addMemberAddress")
    @ResponseBody
    public void addMemberAddress(MemberAddress memberAddress){
        Member m = OrderSessionUtil.getCurrent();
        memberAddress.setMemberId(m.getId());
        System.out.println(memberAddress.getId());
        if (memberAddress.getId() != null) {
            memberAddressService.updataMemberAddressById(memberAddress);
        }else {
            memberAddressService.save(memberAddress);
        }
    }
    @PostMapping("/delMemberAddress")
    @ResponseBody
    public void delMemberAddress(Integer id){
        Member m = OrderSessionUtil.getCurrent();
        memberAddressService.delMemberAddress(id,m.getId());
    }
    @PostMapping("/updataMemberAddressDefaultAddr")
    @ResponseBody
    public void updataMemberAddressDefaultAddr(Integer id){
        memberAddressService.updataMemberAddressDefaultAddr(id);
    }

    @GetMapping("/selectMemberAddress")
    @ResponseBody
    public List<MemberAddress> selectMemberAddress(){
        return memberAddressService.selectMemberAddress();
    }

    @GetMapping("/selMemberAddress")
    @ResponseBody
    public ResultVO selMemberAddress(Integer id){
        return memberAddressService.selMemberAddress(id);
    }


}
